package com.relsell.view;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.relsell.admin.DbHelper;
import com.relsell.pointofsales.R;
import com.relsell.utility.AppVars;
import com.relsell.utility.UserPin;
import com.relsell.utility.UtilitySingleTon;

public class Maps extends Fragment {
	
	GoogleMap map;
	public static boolean isCheckingGooglePlayServices;
	private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
	private boolean goolePlayResult = false;
	static final LatLng MyLocation = new LatLng(30.4351, 77.73051);
	public Maps() {
		
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = null;
		if (checkPlayServices()) {
	        v = inflater.inflate(R.layout.maps, container, false);
		} else {
			if (AppVars.isLogEnabled) {
				Log.i(AppVars.LOG_TAG, "proper google play lib not installed or device  doesn't support");
			}
		}
		return v;
	}

	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		super.onActivityCreated(savedInstanceState);
		map=((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
		UtilitySingleTon.getInstance().setMap(map);
		UtilitySingleTon.getInstance().prepareUserPinList(AppVars.sUserPinList,this.getActivity());
		UtilitySingleTon.getInstance().drawMapOptions(UtilitySingleTon.getInstance().getMap(),this.getActivity(),AppVars.sUserPinList);
	}
	/**
	 * Check the device to make sure it has the Google Play Services APK. If
	 * it doesn't, display a dialog that allows users to download the APK from
	 * the Google Play Store or enable it in the device's system settings.
	 */
	public boolean checkPlayServices() {
	    final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
		if (resultCode != ConnectionResult.SUCCESS) {
			if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
				getActivity().runOnUiThread(new Runnable() {
					public void run() {
						isCheckingGooglePlayServices = true;
						GooglePlayServicesUtil
								.getErrorDialog(resultCode, getActivity(),
										PLAY_SERVICES_RESOLUTION_REQUEST)
								.show();
						goolePlayResult = true;
					}
				});

			} else {
				if (AppVars.isLogEnabled) {
					Log.i(AppVars.LOG_TAG, "This device is not supported.");
				}
				goolePlayResult = false;
			}
		} else {
			isCheckingGooglePlayServices = true;
			goolePlayResult = true;
		}

		return goolePlayResult;
	}
	
	

	
	
	

}
